package ui.popup 
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import ui.textfield.GCTextField;
	
	import ui.button.GCButton;
	
	/**
	 * A generic popup that displays a message to the user
	 */
	public class GCTextPopup extends Sprite 
	{
		private static const DEFAULT_WIDTH:uint = 300;
		private static const DEFAULT_HEIGHT:uint = 250;
		
		private var _width:uint;
		private var _height:uint;
		
		private var _textArea:GCTextField;
		private var _confirmButton:GCButton;
		private var _message:String;
		private var _endFunction:Function;
		
		private var _backgroundImage:Sprite;
		
		/**
		 * Constructor
		 * @param	message
		 * @param	endFunction
		 * @param	width
		 * @param	height
		 */
		public function GCTextPopup(message:String, endFunction:Function, width:uint = DEFAULT_WIDTH, height:uint = DEFAULT_HEIGHT) 
		{
			_textArea = new GCTextField();
			_confirmButton = new GCButton(onClick);
			_message = message;
			_endFunction = endFunction;
			
			_width = width;
			_height = height;
			
			init();
		}
		
		private function init():void
		{			
			this.addChild(_textArea);
			this.addChild(_confirmButton);
			
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("OK");
			
			_textArea.setStyle(GCTextField.TEXT_TYPE_LEFT);
			//_textArea.setBgStyle(GCStylizedText.STYLE_NONE, this.width - 40, this.height - 100);
			_textArea.text = _message;
			_textArea.width = _width - 40;
			_textArea.height = _textArea.textHeight + 50;// _height - 100;
			_textArea.x += 20;
			_textArea.y += 30;
			_confirmButton.x = this.width / 2 - _confirmButton.width / 2;
			//_confirmButton.y = _textArea.y + _textArea.height;
			_confirmButton.y = _height - _confirmButton.height - 20;
			
			//Popup background
			_backgroundImage = GCResources.getMaskedCanvasBackground(_width, _height);
			this.addChildAt(_backgroundImage, 0);
		}

		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------
		
		/**
		 * Handles the event for clicking the ok button
		 * @param	event
		 */
		private function onClick(event:MouseEvent):void
		{
			// Clean up the popup
			_confirmButton.removeEventListener(MouseEvent.CLICK, onClick);
			_confirmButton.disable();
			
			if (_endFunction != null)
			{
				_endFunction();
			}
		}
		
	}
}